#include "stdafx.h"
#include "Tools.h"


CTools::CTools()
{
}


CTools::~CTools()
{
}

void CTools::DrawToBackground(int posx, int posy, IplImage *Bg, IplImage *img, int keyb, int keyg, int keyr)
{
	for (int i = 0; i < img->height; i++) {
		for (int j = 0; j < img->width; j++) {
			int b, g, r;
			b = CV_IMAGE_ELEM(img, uchar, i, j * 3 + 0);
			g = CV_IMAGE_ELEM(img, uchar, i, j * 3 + 1);
			r = CV_IMAGE_ELEM(img, uchar, i, j * 3 + 2);

			if (abs(b - keyb) <= 10 && abs(r - keyr) <= 10 && abs(g - keyg) <= 10)	continue;
			if (i + posy > Bg->height || i + posy < 0 || j + posx > Bg->width || j + posx < 0)	continue;

			CV_IMAGE_ELEM(Bg, uchar, i + posy, (j + posx) * 3 + 0) = b;
			CV_IMAGE_ELEM(Bg, uchar, i + posy, (j + posx) * 3 + 1) = g;
			CV_IMAGE_ELEM(Bg, uchar, i + posy, (j + posx) * 3 + 2) = r;
		}
	}
}

bool CTools::DetectCollision(CvRect r1, CvRect r2)
{
	int absx = abs((r1.width / 2 + r1.x) - (r2.width / 2 + r2.x));
	int absy = abs((r1.height / 2 + r1.y) - (r2.height / 2 + r2.y));

	if (absx < (r1.width + r2.width) / 2 &&
		absy < (r1.height + r2.height) / 2)
		return true;
	else return false;
}



bool CTools::DetectAdjacent(CvPoint pos1, CvPoint pos2)
{
	if (abs(pos1.x - pos2.x) <= 1 && abs(pos1.y - pos2.y) <= 1)
		return true;
	else return false;
}


bool CTools::DetectOverlap(CvPoint pos1, CvPoint pos2)
{
	if (pos1.x == pos2.x && pos1.y == pos2.y)
		return true;
	return false;
}
